//
//  MedicalInfo.m
//  ALittleReminder
//
//  Created by Kevin Basanti on 11-11-18.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "MedicalInfo.h"

@implementation MedicalInfo




-(IBAction)onClickClose:(id)sender{
    
    
    [self dismissModalViewControllerAnimated:YES];
    
}

-(IBAction)edit:(id)sender{
    
    
    
    {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ENTER PASSWORD" message:@"1234" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    mytextfield = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
    CGAffineTransform myTransform =CGAffineTransformMakeTranslation(0, 60);
    [alert setTransform:myTransform];
    [mytextfield setBackgroundColor:[UIColor whiteColor]];
    mytextfield.placeholder = @"password";
        mytextfield.secureTextEntry = YES;
    [alert addSubview:mytextfield];
        [alert show];
    }
    
}    
    - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
        // the user clicked one of the OK/Cancel buttons
        if (buttonIndex == 0)
        {
            
            
            
            
        }
        else
        {
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *docDir = [paths objectAtIndex: 0];
            NSString *docFile = [docDir stringByAppendingPathComponent: @"caretakerInfo.txt"];
            
            NSString *textfromfile = [NSString stringWithContentsOfFile:docFile encoding:NSStringEncodingConversionAllowLossy error:nil];
            
            NSArray *info = [textfromfile componentsSeparatedByString:@"-"];
            
            NSString *pass  = [info objectAtIndex:4];
            
            NSString *passentered	= mytextfield.text;
            
            if([pass isEqualToString:passentered]){
                infoTextView.editable = YES;
                mylabel.text = @"Start Editing";
                mylabel.textColor = [UIColor greenColor];
                mylabel.font = [UIFont systemFontOfSize:16];
            }
            else{
                mylabel.text = @"WRONG PASSWORD";
                mylabel.textColor = [UIColor redColor];
                mylabel.font = [UIFont systemFontOfSize:16];
            }
        }
    }

-(IBAction)done:(id)sender{
    
    
    NSString *infoText = infoTextView.text;
    

    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [paths objectAtIndex: 0];
    NSString *docFile = [docDir stringByAppendingPathComponent: @"info.txt"];
    
    //your variable "deck"
    
    
    mylabel.text = @"";
    
    
    NSError *error;
    if ([infoText writeToFile:docFile atomically:YES encoding:NSStringEncodingConversionAllowLossy error:&error]
        ) {
        NSLog(@"saved");
    }else{
        NSLog(@"not saved: %@",error);
    }
    
    [infoTextView resignFirstResponder];
    infoTextView.editable = NO;
    
    
    
    NSString *textfromfile = [NSString stringWithContentsOfFile:docFile encoding:NSStringEncodingConversionAllowLossy error:nil];
    infoTextView.text = textfromfile;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}



- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [paths objectAtIndex: 0];
    NSString *docFile = [docDir stringByAppendingPathComponent: @"info.txt"];
    
     NSString *textfromfile = [NSString stringWithContentsOfFile:docFile encoding:NSStringEncodingConversionAllowLossy error:nil];
    infoTextView.text = textfromfile;
    // Do any additional setup after loading the view from its nib.
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
